//
//  HJStatusViewModel.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/11.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit

class HJStatusViewModel: NSObject {
    //创建时间
    
    var status: HJStatus?
    //来源字符串
    var sourceTxet: String?
    
    var vipImage: UIImage? {
        
        guard let mbrank = status?.user?.mbrank else{
            
            return nil
        }
        
        if mbrank > 0 && mbrank < 7 {
            
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return nil
    }
    
    
    var verrified: UIImage? {
        
        guard let verrified = status?.user?.verified else {
            return nil
        }
        
        switch verrified {
        
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
        
        
    }
    
    var retweetCountStr = "评论"
    var commentCountStr = "转发"
    var unlikeCountStr = "赞"
    
    var retweetText: String?
    
    var createAtText: String?{
        
        guard let createDate = NSDate.sinaDate(status!.created_at!) else {
            
            return ""
        }
        
        return createDate.sinaDateString
                
    }
    
    init(status: HJStatus) {
        
        super.init()
        self.status = status
        
        retweetCountStr = getCountString(self.status?.reposts_count ?? 0 , defaulTitle:"评论")
        commentCountStr = getCountString(self.status?.reposts_count ?? 0 ,defaulTitle:"转发")
        unlikeCountStr = getCountString(self.status?.reposts_count ?? 0 ,defaulTitle:"赞")
        
        //计算转发微博的逻辑
        if let retweetStatus = status.retweeted_status {
            
            if let name = retweetStatus.user?.name {
              retweetText = "\(name):\(retweetStatus.text!))"
            }
        }
        
        dealSourceText()
        dealCreateAtText(status.created_at!)
    }
    //处理来源字符串
    private func dealSourceText(){
        
        guard let source = status?.source else {
            return
        }
        
        if let preRange = source.rangeOfString("\">"){
            
            if let sufRange = source.rangeOfString("</"){
                
                sourceTxet = "来自\(source.substringWithRange(preRange.endIndex..<sufRange.startIndex))"
            }
        }
    }
    //处理创建时间
    private func dealCreateAtText(create_at: String) {
        
//        func isDateInThisYear(targetDate: NSData) -> Bool {
//            let comp = calendar.components(.Year, fromDate: targetDate, toDate: NSDate(), options: [])
//            return comp.year < 1
        
        
    }
    
       
    
    private func getCountString(count:Int,defaulTitle:String)->String{
        
        
        
        if count > 0 {
            
            if count < 10000 {
                
                retweetCountStr = "\(count)"
            }else{
                
                let result = CGFloat(count / 1000) / 10
                let resultStr = "\(result)万"
                
                if resultStr.containsString(".0"){
                    
                    return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                
                return resultStr
            }
        }
        
        return defaulTitle

    }
    
    override var description:String {
        
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
